/**
* 版权所有:版权所有(C) 2020，卓信科技
* 系统名称:FOL2020-财务在线
* 文件名称:SysUtil.java
* 模块名称:TODO(这里请补充模块名称)
* 创建日期:2020年5月19日
* 作        者:Baisong
* 内容摘要:TODO(这里请补充详细的描述信息)
*/
package com.zx.emanage.util.gen;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zx.emanage.FOLApplication;
import com.zx.emanage.sysmanage.persist.SysLogDao;
import com.zx.emanage.user.service.IUserService;
import com.zx.emanage.user.vo.PmPersonnel;
import com.zx.emanage.util.gen.entity.SysLog;
import com.zx.sframe.util.file.GlobalFileUtil;

/**
 * 类  名  称:SysUtil
 *
 * 内容摘要:TODO(这里详细描述这个类的作用)
 *
 * 作        者:Baisong
 */
public class SysUtil 
{

	public static final String SSO_MODULE = "SSO_MODULE";

    public static final String APP_NAME = "FOL";

    public synchronized static String makeUUID()
    {
		return UUID.randomUUID().toString().replaceAll("-", "_");
	}

    /**
     * 
     * @Title: saveLog
     * @Description: TODO(保存日志)
     * @param sysLog
     * @param userBean 
     * @author: Quwenrui
     * @time:2018年6月14日 下午3:46:50
     * history:
     * 1、2018年6月14日 Quwenrui 创建方法
     */
	public static void saveLog(SysLog sysLog, PmPersonnel pmPersonnel)
    {
        // 获取当前时间
        Timestamp date = new Timestamp(System.currentTimeMillis());
        SysLogDao sysLogDao = (SysLogDao) FOLApplication.getBean(SysLogDao.class);
		IUserService userService = (IUserService) FOLApplication.getBean(IUserService.class);
      //判断
        if (sysLog != null)
        {
			// 如果为空尝试用缓存中获取
        	if(pmPersonnel==null) 
        	{
				RequestAttributes ra = RequestContextHolder.getRequestAttributes();
				HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
				pmPersonnel = userService.getPmPersonnel(request.getHeader(GlobalVal.XTOKEN));
        	}
        	//判断
			if (pmPersonnel != null)
            {
				// 员工id
				if(pmPersonnel.getPersonnel_id()!=null) 
				{
					sysLog.setUser_id(pmPersonnel.getPersonnel_id());	
				}
                // 员工编号
				sysLog.setUser_name(pmPersonnel.getPersonnel_name());
                // 员工姓名
				sysLog.setUser_code(pmPersonnel.getPersonnel_shortCode());
                // 操作时间
                sysLog.setOper_timestamp(date);
                sysLog.setOper_ip(getIP());
                sysLogDao.saveLog(sysLog);
            }
        }
    }
    /**
     * 获取ip
     */
    public static String getIP()
    {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
        String ip = request.getHeader("x-forwarded-for");
      //判断
        if (!checkIP(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
      //判断
        if (!checkIP(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
      //判断
        if (!checkIP(ip))
        {
            ip = request.getRemoteAddr();
        }
      //判断
        if ("0:0:0:0:0:0:0:1".equals(ip))
        {
            ip = "127.0.0.1";
        }
        return ip;
    }
/**
 * 检查
 */
    private static boolean checkIP(String ip)
    {
    	//判断
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip) || ip.split("\\.").length != 4)
        {
            return false;
        }
        return true;
    }
    
    /** 
    * 获取过去第几天的日期 
    * zw
    * @param past 
    * @return 
    */  
   public static String getPastDate(int past) 
   {  
       Calendar calendar = Calendar.getInstance();  
       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
       Date today = calendar.getTime();  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String result = format.format(today);  
       return result;  
   }  
  
   /** 
    * 获取未来 第 past 天的日期 
    * zw
    * @param past 
    * @return 
    */  
   public static String getFetureDate(int past) 
   {  
       Calendar calendar = Calendar.getInstance();  
       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
       Date today = calendar.getTime();  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String result = format.format(today);  
       return result;  
   }  

	/**
	 * 异步压缩文件
	 */

	public static void asyncCompress(final String url)
	{
		new Thread() 
		{
			public void run() 
			{
				GlobalFileUtil.saveImgCompress(url);
			}
		}.start();
	}

    /**
     * 根据map里的key替换形如【#need_replace_value#】参数(need_replace_value)的值
     */
    public static String replaceParamValueByMap(String oldString, Map<String, String> paramMap)
    {
        // 判断
        if (paramMap == null)
        {
            return oldString;
        }
        // 循环
        for (String key : paramMap.keySet())
        {
            // 判断
            if (paramMap.get(key) != null)
            {
                oldString = oldString.replace("#" + key + "#", paramMap.get(key));
            }
        }
        return oldString;
    }
}
